package com.example.myapplication10.Activities;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.myapplication10.Buttons.Btn_AQI;
import com.example.myapplication10.Buttons.Btn_extract15Days;
import com.example.myapplication10.Buttons.Btn_smallAQI;
import com.example.myapplication10.DTO.ForecastDTO;
import com.example.myapplication10.DTO.WeatherDTO;
import com.example.myapplication10.MsgEvents.MsgEventTitle;
import com.example.myapplication10.MsgEvents.MsgEventUpdateOver;
import com.example.myapplication10.MsgEvents.MsgEventWeatherUpdate;
import com.example.myapplication10.R;
import com.example.myapplication10.Utils.IObserver;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class WeatherFrag1 extends Fragment implements IObserver {
    //是否可见
    public boolean isVisible = true;
    //是否初始化完成
    public boolean isInit = false;
    //是否已经加载过
    public boolean isLoadOver = false;
    private int shownId;
    WeatherImg weatherImg = new WeatherImg();
    View view;
    Btn_AQI btn_aqi;
    List<TextView>textViewList = new ArrayList<>();
    List<Btn_smallAQI>btnSmallAQIS = new ArrayList<>();
    List<String>strings = new ArrayList<>();
    WeatherDTO weather;
    TextView tvDegree;TextView tvType;TextView tvH;TextView tvL;
    TextView tv01,tv02,tv03,tv11,tv12,tv13;
    Btn_smallAQI btn1;
    Btn_smallAQI btn2;
    Btn_smallAQI btn3;
    Btn_extract15Days btnExtract15Days;
    /**
     * 初始化一些参数，完成懒加载和数据只加载一次的效果
     * isInit = true：此Fragment初始化完成
     * isLoadOver = false：此Fragment没有加载过
     * isVisible = true：此Fragment可见
     */


    public WeatherFrag1(WeatherDTO weather0){
        this.weather = weather0;
//        switch (index) {
//            case 0:
//                shownId = R.id.shown1;
//                break;
//            case 1:
//                shownId = R.id.shown2;
//                break;
//            case 2:
//                shownId = R.id.shown3;
//                break;
//            case 3:
//                shownId = R.id.shown4;
//                break;
//            case 4:
//                shownId = R.id.shown5;
//                break;
//            case 5:
//                shownId = R.id.shown6;
//                break;
//            case 6:
//                shownId = R.id.shown7;
//                break;
//            case 7:
//                shownId = R.id.shown8;
//                break;
//            default:
//                shownId = R.id.shown1;
//                break;
//        }
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.weather_fragment,null);
            isInit = true;
        }
        EventBus.getDefault().register(this);
//        imageView = view.findViewById(shownId);
//        imageView.setImageResource(R.mipmap.is_shown);
//        Context context = getContext();
        btn_aqi = view.findViewById(R.id.btn_AQI);
        if(this.weather!=null){
            tvDegree = view.findViewById(R.id.degree);
            tvType = view.findViewById(R.id.type);
            tvH = view.findViewById(R.id.high);
            tvL = view.findViewById(R.id.low);
            tv01 = view.findViewById(R.id.day1_weather);
            tv02 = view.findViewById(R.id.day2_weather);
            tv03 = view.findViewById(R.id.day3_weather);
            tv11 = view.findViewById(R.id.degree_1);
            tv12 = view.findViewById(R.id.degree_2);
            tv13 = view.findViewById(R.id.degree_3);
            btn1 = view.findViewById(R.id.AQI_1);
            btn2 = view.findViewById(R.id.AQI_2);
            btn3 = view.findViewById(R.id.AQI_3);
            btnExtract15Days = view.findViewById(R.id.btn_extract15days);
            create();
        }
        return view;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        return super.onContextItemSelected(item);
    }

    @Nullable
    @Override
    public View getView() {
        return view;
    }

    public void setView(View view) {
        this.view = view;
    }


    public void create() {
        WeatherDTO weather = this.weather;
        btnExtract15Days.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MsgEventTitle("近15日天气"));
            }
        });
//        MsgEventWeather weatherEvent = new MsgEventWeather(weather);
//        weatherEvent.setMsg(weather);
//        EventBus.getDefault().post(weatherEvent);
        List<ForecastDTO> forecastList = weather.getData().getForecast();
        String currentDegree = weather.getData().getWendu();
//        String currentHumi = weather.getData().getShidu();
        String high = forecastList.get(0).getHigh();
        String low = forecastList.get(0).getLow();
        String type = forecastList.get(0).getType();
//        MsgEventAQI AQI = new MsgEventAQI(forecastList.get(0).getAqi());
//        AQI.setMsg(forecastList.get(0).getAqi());
//        EventBus.getDefault().post(AQI);
        btn_aqi.update(forecastList.get(0).getAqi());
        tvDegree.setText(currentDegree);
//        tvDegree.setText("-35");
        tvType.setText(type);
        tvH.setText(high.replace("高温 ",""));
        tvL.setText(low.replace("低温 ",""));
        ForecastDTO forecastDTO = new ForecastDTO();
        textViewList.add(tv01);textViewList.add(tv11);textViewList.add(tv02);
        textViewList.add(tv12);textViewList.add(tv03);textViewList.add(tv13);
        btnSmallAQIS.add(btn1);btnSmallAQIS.add(btn2);btnSmallAQIS.add(btn3);
        strings.add("今天");strings.add("明天");strings.add("后天");
        for (TextView textView:textViewList){
            int index = textViewList.indexOf(textView);
            if(index % 2 == 0){
                forecastDTO = forecastList.get(index/2);
                textView.setText(String.format("     %s %s",strings.get(index/2),forecastDTO.getType()));
                textView.setCompoundDrawablesWithIntrinsicBounds
                        (0,0,weatherImg.getWeatherImg(forecastDTO.getType()),0);
                btnSmallAQIS.get(index/2).update(forecastDTO.getAqi());
            }
            else{
                textView.setText(String.format("%s° / %s°     ",
                    forecastDTO.getHigh().replace("高温 ","").replace("℃",""),
                    forecastDTO.getLow().replace("低温 ","").replace("℃","")));
            }
        }
        isLoadOver = true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void messageWeatherUpdateEventBus(MsgEventWeatherUpdate event) {
        List<ForecastDTO> forecastList = weather.getData().getForecast();
        ForecastDTO forecastDTO = new ForecastDTO();
        for (TextView textView:textViewList){
            int index = textViewList.indexOf(textView);
            if(index % 2 == 0){
                forecastDTO = forecastList.get(index/2);
                textView.setText(String.format("     %s %s",strings.get(index/2),forecastDTO.getType()));
                textView.setCompoundDrawablesWithIntrinsicBounds
                        (0,0,weatherImg.getWeatherImg(forecastDTO.getType()),0);
                btnSmallAQIS.get(index/2).update(forecastDTO.getAqi());
            }
            else{
                textView.setText(String.format("%s° / %s°     ",
                        forecastDTO.getHigh().replace("高温 ","").replace("℃",""),
                        forecastDTO.getLow().replace("低温 ","").replace("℃","")));
            }
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
//        EventBus.getDefault().post(new MsgEventUpdate(weather1.getCityInfo().getUpdateTime()));
        EventBus.getDefault().post(new MsgEventUpdateOver(LocalTime.now().format(formatter)));
//        EventBus.getDefault().post(new MsgEventUpdateOver(""));
        isLoadOver = true;
    }


    @Override
    public void updateDataSet(WeatherDTO weather) {

    }
}
